<?php
class SearchController extends Controller{
    
    public function actionSearch_sort(){
        if(Yii::app()->request->isAjaxRequest){
            if(isset($_POST['moinhat'])){
                Yii::app()->clientScript->scriptMap=array('jquery.js'=>false);
                Yii::app()->clientScript->scriptMap=array('jquery.min.js'=>false);
                
                $criteria = new CDbCriteria;
                $criteria->order='id ASC';
        
                $dataProvider=new CActiveDataProvider('Service',
                    array(
                        'criteria'=>$criteria,
                        'pagination'=>array(
                        'pageSize'=>5,
                    ),
                ));
                $this->renderPartial('search_form',array('dataProvider'=>$dataProvider),false,true);
                Yii::app()->end();
            }
        }
    }
    public function actionSearch_form(){
        if(Yii::app()->request->isAjaxRequest){
            if(isset($_POST['input-search-service'])){
                $keyword = $_POST['input-search-service']; 
                $name_controller = $_POST['controller'];
                $name_action = $_POST['action'];
                if($name_controller == 'show' and $name_action == 'index'){
                    if(isset($_POST['id'])){
                        $id = $_POST['id'];
                        $criteria = new CDbCriteria;
                        $criteria->condition='servicetype='.$id;
                        $criteria->compare('name',$keyword, true);
                        
                        $dataProvider=new CActiveDataProvider('Service',array(
                            'criteria'=>$criteria,
                            'pagination'=>array(
                                'pageSize'=>5,
                            ),
                        ));
                    }else{
                        $criteria = new CDbCriteria;
                        $criteria->compare('name',$keyword, true);
                        $dataProvider=new CActiveDataProvider('Service',array(
                            'criteria'=>$criteria,
                            'pagination'=>array(
                                'pageSize'=>5,
                            ),
                        ));
                    }
                    $this->renderPartial('search_form',array('dataProvider'=>$dataProvider),false,true);
                    Yii::app()->end();
                }
                /**
                *Search tag
                **/
                else if($name_controller == 'show' and $name_action == 'tag'){
                        if(isset($_POST['tag'])){
                            $tag = $_POST['tag'];
                            $criteria_tag = new CDbCriteria;
                            $criteria_tag->order= "time_create DESC";
                            $criteria_tag->compare('keyword',$tag,true);
                            $criteria_tag->compare('name',$keyword, true);
                            
                            $dataProvider=new CActiveDataProvider('Service',
                                array(
                                    'criteria'=>$criteria_tag,
                                    'pagination'=>array(
                                        'pageSize'=>5,
                                    ),
                                )
                            );
                            
                            $this->renderPartial('search_form',array('dataProvider'=>$dataProvider),false,true);
                            Yii::app()->end();
                        }else{
                            throw new CHttpException(404,'The requested page does not exist.');
                        }
                }// end search tag
                
            }
        }
    }
}
?>